package com.example.springpethospital.entity.examination.dto;

import com.example.springpethospital.entity.examination.po.ExaminationPO;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.BeanUtils;

@Slf4j
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ExaminationDTO extends ExaminationPO {

    private ExaminationPaperDTO paper;

    /**
     * todo student dto
     */
    private List<Object> studentList;


    public static List<ExaminationDTO> convert(List<ExaminationPO> examinations, List<ExaminationPaperDTO> papers) {
        if (CollectionUtils.isEmpty(examinations)) {
            return Collections.emptyList();
        }
        Map<Integer, ExaminationPaperDTO> paperMap = ListUtils.emptyIfNull(papers).stream()
            .collect(Collectors.toMap(ExaminationPaperDTO::getId, Function.identity(), (a, b) -> a));
        return examinations.stream().map(e -> {
            ExaminationPaperDTO paper = paperMap.get(e.getPaperId());
            if (paper == null) {
                log.error("有试卷被删除了, 此处过滤, paperId = {}", e.getPaperId());
                return null;
            }
            ExaminationDTO examinationDTO = new ExaminationDTO();
            BeanUtils.copyProperties(e, examinationDTO);
            examinationDTO.setPaper(paper);
            examinationDTO.setStudentList(Collections.emptyList());
            return examinationDTO;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

}
